﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using DBUtility;
using MySql.Data.MySqlClient;

namespace DAL
{
    public class DServerConfig
    {

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string servername)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tserverconfig");
            strSql.Append(" where ServerName=@ServerName");
            MySqlParameter[] parameters = {
					new MySqlParameter("@ServerName", MySqlDbType.String)
          };
            parameters[0].Value = servername;

            return DbHelperMySQL.Exists(strSql.ToString(), parameters);
        }

 
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.ServerConfigInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tserverconfig(");
            strSql.Append("ServerName,LongitudeOffset,LatitudeOffset,DNSURL)");
            strSql.Append(" values (");
            strSql.Append("@ServerName,@LongitudeOffset,@LatitudeOffset,@DNSURL)");
            MySqlParameter[] parameters = {
					new MySqlParameter("@ServerName", MySqlDbType.VarChar,20),
					new MySqlParameter("@LongitudeOffset", MySqlDbType.VarChar,8),
					new MySqlParameter("@LatitudeOffset", MySqlDbType.VarChar,8),
                    new MySqlParameter("@DNSURL", MySqlDbType.VarChar,100)};
            parameters[0].Value = model.ServerName;
            parameters[1].Value = model.LongitudeOffset;
            parameters[2].Value = model.LatitudeOffset;
            parameters[3].Value = model.DNSURL;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }            
                return false;            
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.ServerConfigInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tserverconfig set ");
            strSql.Append("ServerName=@ServerName,");
            strSql.Append("LongitudeOffset=@LongitudeOffset,");
            strSql.Append("LatitudeOffset=@LatitudeOffset,");
            strSql.Append("DNSURL=@DNSURL");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
				    new MySqlParameter("@ServerName", MySqlDbType.VarChar,20),
					new MySqlParameter("@LongitudeOffset", MySqlDbType.VarChar,8),
					new MySqlParameter("@LatitudeOffset", MySqlDbType.VarChar,8),
                    new MySqlParameter("@DNSURL", MySqlDbType.VarChar,100),
					new MySqlParameter("@id", MySqlDbType.Int32,10)};
            parameters[0].Value = model.ServerName;
            parameters[1].Value = model.LongitudeOffset;
            parameters[2].Value = model.LatitudeOffset;
            parameters[3].Value = model.DNSURL;
            parameters[4].Value = model.id;
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }            
                return false;            
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tserverconfig ");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)
};
            parameters[0].Value = id;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tserverconfig ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.ServerConfigInfo GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,ServerName,LongitudeOffset,LatitudeOffset,DNSURL ");
            strSql.Append("from tserverconfig where id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)
};
            parameters[0].Value = id;

            Model.ServerConfigInfo model = new Model.ServerConfigInfo();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ServerName"] != null && ds.Tables[0].Rows[0]["ServerName"].ToString() != "")
                {
                    model.ServerName = ds.Tables[0].Rows[0]["ServerName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["LongitudeOffset"] != null && ds.Tables[0].Rows[0]["LongitudeOffset"].ToString() != "")
                {
                    model.LongitudeOffset = ds.Tables[0].Rows[0]["LongitudeOffset"].ToString();
                }
                if (ds.Tables[0].Rows[0]["LatitudeOffset"] != null && ds.Tables[0].Rows[0]["LatitudeOffset"].ToString() != "")
                {
                    model.LatitudeOffset =ds.Tables[0].Rows[0]["LatitudeOffset"].ToString();
                }
                if (ds.Tables[0].Rows[0]["DNSURL"] != null && ds.Tables[0].Rows[0]["DNSURL"].ToString() != "")
                {
                    model.DNSURL = ds.Tables[0].Rows[0]["DNSURL"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.ServerConfigInfo GetModelByName(string servername)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,ServerName,LongitudeOffset,LatitudeOffset,DNSURL ");
            strSql.Append("from tserverconfig where ServerName=@ServerName");
            MySqlParameter[] parameters = {
					new MySqlParameter("@ServerName", MySqlDbType.VarChar,20)
             };
            parameters[0].Value = servername;

            Model.ServerConfigInfo model = new Model.ServerConfigInfo();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ServerName"] != null && ds.Tables[0].Rows[0]["ServerName"].ToString() != "")
                {
                    model.ServerName = ds.Tables[0].Rows[0]["ServerName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["LongitudeOffset"] != null && ds.Tables[0].Rows[0]["LongitudeOffset"].ToString() != "")
                {
                    model.LongitudeOffset =  ds.Tables[0].Rows[0]["LongitudeOffset"].ToString();
                }
                if (ds.Tables[0].Rows[0]["LatitudeOffset"] != null && ds.Tables[0].Rows[0]["LatitudeOffset"].ToString() != "")
                {
                    model.LatitudeOffset =  ds.Tables[0].Rows[0]["LatitudeOffset"].ToString();
                }
                if (ds.Tables[0].Rows[0]["DNSURL"] != null && ds.Tables[0].Rows[0]["DNSURL"].ToString() != "")
                {
                    model.DNSURL = ds.Tables[0].Rows[0]["DNSURL"].ToString();
                }
                return model;
            }            
                return null;           
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,ServerName,LongitudeOffset,LatitudeOffset,DNSURL");
            strSql.Append(" FROM tserverconfig ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperMySQL.Query(strSql.ToString());
        }

    }
}
